<?php

namespace app\models;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%category}}".
 *
 * @property int $id 分类id
 * @property int $pid 父类id
 * @property string $name 分类名称
 * @property string $desc 分类描述
 * @property int $project_id 项目id
 * @property int $created_uid 创建者uid 处理一个项目授权给多个用户
 * @property string $created_time 创建时间
 * @property int $sort 序号
 * @property int $is_del 是否删除 0正常 , 1删除
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'project_id','created_time','created_uid', 'sort', 'is_del'], 'integer'],
            [['name', 'desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => 'Pid',
            'name' => 'Name',
            'desc' => 'Desc',
            'project_id' => 'Project ID',
            'created_uid' => 'Created Uid',
            'created_time' => 'Created Time',
            'sort' => 'Sort',
            'is_del' => 'Is Del',
        ];
    }

    /**
     * @param string $uid
     * @return array|\yii\db\ActiveRecord[]
     * 获取指定用户创建的分类
     */
    public static function getCategoryByWhere($map)
    {
        if (isset($map['created']) && !empty($map['created']))$map['uid'] = Yii::$app->user->id;
        $category = self::find()->where($map)->asArray()->all();

        return $category;
    }


    /**
     * 分类格式化
     * 数据格式
     * [
     *     {
     *         text: "帮我8接口文档",
     *         nodes: [
     *             {
     *                 text: "Child 2"
     *             }
     *         ]
     *     }
     * ];
     */
    public static function formatCategory($category , $pid = 0 ,$name='nodes')
    {
        $returnData = [];
        array_walk($category,function ($v) use (&$returnData , $pid , $name , $category){
            if($v['pid'] == $pid){
                $temp['id'] = $v['id'];
                $temp['text'] = $v['name'];
                $temp['type'] = 'category';
                $temp['href'] = '';
//                $temp['href'] = Url::toRoute(['page/index','projectId' => $v['project_id'] , 'categoryId' => $v['id']]);
                $checkData = self::formatCategory($category , $v['id'] ,$name);
                empty($checkData) && $checkData = Page::getFormatPage(['cate_id' => $v['id']]);
                $temp[$name] = $checkData;
                $returnData[] = $temp;
            }
        });
        return $returnData;
    }

    /**
     * 根据当前分类的id 获取父级id
     * @param $cid
     */
    public static function parentId($cid)
    {
        if(empty($cid))return '';
        $cateInfo = static::find()->where(['id' => $cid])->asArray()->one();

        if(empty($cateInfo))return '';

        return isset($cateInfo['pid']) ? $cateInfo['pid'] : '';
    }
}
